package cn.yoyo.service;

import cn.yoyo.entity.Book;
import cn.yoyo.entity.LargeFile;
import cn.yoyo.mapper.BookMapper;
import cn.yoyo.mapper.LargeFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class BookServiceImpl implements BookService {
    @Autowired(required = false)
    BookMapper bookMapper;
    @Autowired(required = false)
    LargeFileMapper largeFileMapper;


    @Override
    public List<Book> findAll() {
        return bookMapper.findAll();
    }

    @Override
    public Book findById(Integer bookid) {
        return bookMapper.findById(bookid);
    }

    @Override
    public List<Book> findByPage(Integer start, Integer num) {
        // 当前页数和每页和每页个数不能小于1
        if (start < 1 || num < 1) return null;
        Integer pageNow = 1;
        if (start > 0) {
            pageNow = (start - 1) * num;
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("start", pageNow);
        map.put("num", num);
        return bookMapper.findByPage(map);
    }

    @Override
    @Transactional
    public int add(Book book, MultipartFile file) {
        book.setPicture(UUID.randomUUID().toString());
        try {
            largeFileMapper.add(new LargeFile(book.getPicture(), file.getOriginalFilename(), file.getBytes()));
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return bookMapper.add(book);
    }

    @Override
    @Transactional
    public int update(Book book, MultipartFile file) {
        // 修改书籍信息 如果要修改书籍图片且原先图片不为空,则先添加再删除旧照片
        String picture = book.getPicture();
        if (file != null && file.getSize() > 1) {
            book.setPicture(UUID.randomUUID().toString());
            try {
                largeFileMapper.add(new LargeFile(book.getPicture(), file.getOriginalFilename(), file.getBytes()));
                if (picture != null && !("".equals(picture))) {
                    largeFileMapper.delete(picture);// 删除旧照片
                }
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
        return bookMapper.update(book);
    }

    @Override
    public int totalPage(Map<String, Object> map) {
        return bookMapper.totalPage(map);
    }

    @Override
    public List<Book> findIf(Integer start, Integer num, String keyWord) {
        // 当前页数和每页和每页个数不能小于1
        if (start < 1 || num < 1) return null;
        Integer pageNow = 0;
        if (start >= 1) {
            pageNow = (start - 1) * num;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageNow);
        map.put("num", num);
        map.put("keyWord", keyWord);
        return bookMapper.findIf(map);
    }

    @Override
    public List<Book> findBookByPage(Integer start, Integer num, Integer typeid) {
        // 当前页数和每页和每页个数不能小于1
        if (start < 1 || num < 1) return null;
        Integer pageNow = 1;
        if (start > 0) {
            pageNow = (start - 1) * num;
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("start", pageNow);
        map.put("num", num);
        map.put("typeid", typeid);
        return bookMapper.findBookByPage(map);
    }


    @Override
    public List<Book> findRate5() {
        return bookMapper.findRate5();
    }

    @Override
    public List<Book> findSale5() {
        return bookMapper.findSale5();
    }

    @Override
    public List<Book> findFav5() {
        return bookMapper.findFav5();
    }

    @Override
    public List<Book> findBookByPageRate(Integer start, Integer num, Integer typeid) {
        // 当前页数和每页和每页个数不能小于1
        if (start < 1 || num < 1) return null;
        Integer pageNow = 1;
        if (start > 0) {
            pageNow = (start - 1) * num;
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("start", pageNow);
        map.put("num", num);
        map.put("typeid", typeid);
        return bookMapper.findBookByPageRate(map);
    }

    @Override
    public List<Book> findBookByPageSale(Integer start, Integer num, Integer typeid) {
        // 当前页数和每页和每页个数不能小于1
        if (start < 1 || num < 1) return null;
        Integer pageNow = 1;
        if (start > 0) {
            pageNow = (start - 1) * num;
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("start", pageNow);
        map.put("num", num);
        map.put("typeid", typeid);
        return bookMapper.findBookByPageSale(map);
    }

    @Override
    public List<Book> findBookByPageFav(Integer start, Integer num, Integer typeid) {
        // 当前页数和每页和每页个数不能小于1
        if (start < 1 || num < 1) return null;
        Integer pageNow = 1;
        if (start > 0) {
            pageNow = (start - 1) * num;
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("start", pageNow);
        map.put("num", num);
        map.put("typeid", typeid);
        return bookMapper.findBookByPageFav(map);
    }

    @Override
    public Object showImg(String id) {
        LargeFile largeFile = largeFileMapper.findById(id);
        if (largeFile == null || largeFile.getContent() == null) return null;
        return largeFile.getContent();
    }

    @Override
    public int delete(Integer id) {
        return bookMapper.delete(id);
    }
}
